package com.genghis.rapidcrm.web.view.support;

import com.genghis.rapidcrm.util.RapidResourceUtil;
import edu.jetwinner.common.EasyStringUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xulixin
 */
public class RapidThemeHandler {

    public static final String RAPID_THEME_HANDLER_KEY = "themeHandler";

    private static final String DEFAULT_LAYOUT = "vlayout";
    private static final String DEFAULT_SKIN_NAME = "softed";

    private String skinName = DEFAULT_SKIN_NAME;

    private String currentLayout = DEFAULT_LAYOUT;

    public RapidThemeHandler() {
        this.skinName = DEFAULT_SKIN_NAME;
    }

    public RapidThemeHandler(String themeName) {
        this.skinName = themeName;
    }

    public String getSkinName() {
        return skinName;
    }

    public void setSkinName(String skinName) {
        this.skinName = skinName;
    }

    public String getLayoutName() {
        return currentLayout;
    }

    public String getStylePath(String filename) {
        String myFileName = filename;
        // Default CSS for better performance, LESS format for development.
        if (EasyStringUtil.isBlank(filename)) {
            myFileName = "style.css";
        }
        String filePath = getThemePath() + "/" + myFileName;
        String fallbackPath = getBaseThemePath() + "/" +
                getDefaultThemeName() + "/" + "style.less";

        String completeFilePath = RapidLoader.resolveNameToPath("~" + filePath);
        String completeFallBackPath = RapidLoader.resolveNameToPath("~" + fallbackPath);

        if (RapidResourceUtil.resourceExists(completeFilePath)) {
            return filePath;
        } else if (RapidResourceUtil.resourceExists(completeFallBackPath)) {
            return fallbackPath;
        }
        // Exception should be thrown???
        return filename + " error! css file not found.";
    }

    /**
     * Function to get the image path
     * This checks image in selected theme if not in images folder if it doest nor exists either case will retutn false
     *
     * @param imageFileName - file name with extension
     * @return <string/boolean> - returns file path if exists or false;
     */
    public String getImagePath(String imageFileName) {
        String imageFilePath = getThemePath() + "/" + "images" + "/" + imageFileName;
        String fallbackPath = getBaseThemePath() + "/" + "images" + "/" + imageFileName;

        if (RapidResourceUtil.resourceExists(imageFilePath)) {
            return imageFilePath;
        } else if (RapidResourceUtil.resourceExists(fallbackPath)) {
            return fallbackPath;
        }
        return imageFileName;
    }

    /**
     * Function to get the Base Theme Path, until theme folder not selected theme folder
     *
     * @return <string> - theme folder
     */
    private String getBaseThemePath() {
        return "/layouts/" + getLayoutName() + "/skins";
    }

    public String getThemePath() {
        return getThemePath(null);
    }

    /**
     * Function to get the selected theme folder path
     *
     * @return <string> -  selected theme path
     */
    private String getThemePath(String theme) {
        if (EasyStringUtil.isBlank(theme)) {
            theme = getDefaultThemeName();
        }

        String selectedThemePath = getBaseThemePath() + "/" + theme;
        String fallBackThemePath = getBaseThemePath() + "/" + getDefaultThemeName();

        String completeSelectedThemePath = RapidLoader.resolveNameToPath("~" + selectedThemePath);
        String completeFallBackThemePath = RapidLoader.resolveNameToPath("~" + fallBackThemePath);

        if (RapidResourceUtil.resourceExists(completeSelectedThemePath)) {
            return selectedThemePath;
        } else if (RapidResourceUtil.resourceExists(completeFallBackThemePath)) {
            return fallBackThemePath;
        }

        return theme + " error! theme not found.";
    }

    /**
     * Function to get the default theme name
     *
     * @return <String> - Default theme name
     */
    private String getDefaultThemeName() {
        return EasyStringUtil.isBlank(this.skinName) ? DEFAULT_SKIN_NAME : this.skinName;
    }

    /**
     * Function returns the current users skin(theme) path
     */
    public String getCurrentUserThemePath(HttpServletRequest request) {
        String themeName = getDefaultThemeName();
        String baseLayoutPath = getBaseThemePath();
        return baseLayoutPath + "/" + themeName;
    }
}
